#include "stm32f10x.h"
#include "bsp_led.h"
#include "systick.h"
#include "bsp_buzz.h"   
#include "bsp_key.h"

uint8_t KEY1_SCAN(void)
{
	static uint8_t key1_old=1;
	uint8_t key1_now;
	key1_now=GETKEY1();
	if(key1_old==1 &&key1_now==0)
	{
		key1_old=key1_now;return 1;
	}
	else 
	{
		key1_old=key1_now;return 0;
	}
}
uint8_t KEY2_SCAN(void)
{
	static uint8_t key2_old=1;
	uint8_t key2_now;
	key2_now=GETKEY2();
	if(key2_old==1 &&key2_now==0)
	{
		key2_old=key2_now;return 1;
	}
	else 
	{
		key2_old=key2_now;return 0;
	}
}
uint8_t KEY_Micro_SCAN(void)
{
	static uint8_t key_micro_old=1;
	uint8_t key_micro_now;
	key_micro_now=GETKEY_Micro();
	if(key_micro_old==1 && key_micro_now==0)
	{
		key_micro_old=key_micro_now;return 1;
	}
	else 
	{
		key_micro_old=key_micro_now;return 0;
	}
}
static uint8_t key1_pressed=0;
void KEY1_SCAN_FLAG(void)
{
	static uint8_t key1_old = 1;
	uint8_t key1_now;
	key1_now=GETKEY1();
	if(key1_old==1 &&key1_now==0)
		key1_pressed=1;
	key1_old=key1_now;
}
static uint8_t key2_pressed=0;
void KEY2_SCAN_FLAG(void)
{
	static uint8_t key2_old = 1;
	uint8_t key2_now;
	key2_now=GETKEY2();
	if(key2_old==1 &&key2_now==0)
		key2_pressed=1;
	key2_old=key2_now;
}
static uint8_t key_micro_pressed=0;
void KEY_Micro_SCAN_FLAG(void)
{
	static uint8_t key_micro_old = 1;
	uint8_t key_micro_now;
	key_micro_now=GETKEY_Micro();
	if(key_micro_old==1 &&key_micro_now==0)
		key_micro_pressed=1;
	key_micro_old=key_micro_now;
}

#define LED_NUM_1    1
#define LED_NUM_2    2
#define LED_NUM_4    4
void LED_Task_1bit(uint8_t LED_num)
{
	if(LED_num != LED_NUM_1 && LED_num != LED_NUM_2 && LED_num != LED_NUM_4)
	{
		LED_RED_OFF();
        LED_GEN_OFF();
        LED_WIT_OFF();
        return;
	}
		LED_RED_OFF();
        LED_GEN_OFF();
        LED_WIT_OFF();
	switch(LED_num)
    {
        case LED_NUM_1: LED_RED_ON(); break;
        case LED_NUM_2: LED_GEN_ON(); break;
        case LED_NUM_4: LED_WIT_ON(); break;
        default:					  break;
    }
}
void BUZZ_Once_ON(void)
{
    BUZZ_ON();
    SysTick_Delay_Nms(500);  
    BUZZ_OFF();
}
#define LED_BLU_T    10
#define LED_BLU_TON  5
void LED_BLU_Blink(void)
{
	static uint16_t T_cnt = 0;  
    
    if (++T_cnt < LED_BLU_TON) 
		{        
			LED_BLU_ON();   
		} 
		else if (T_cnt < LED_BLU_T) 
		{        
			LED_BLU_OFF(); 
		} 
		else 
		{     
			T_cnt = 0;
		}
}
int main(void)
{
	static uint8_t micro_key_work = 1;
	static uint8_t KEY1_CNT=0;
	static uint8_t KEY2_CNT=0;
	LED_GPIO_Config();
	BUZZ_GPIO_Config();
	KEY_GPIO_Config();
	LED_ALL_OFF();
	BUZZ_OFF();
	while(1)
	{
		KEY1_SCAN_FLAG();
		KEY2_SCAN_FLAG();
		KEY_Micro_SCAN_FLAG();
		LED_BLU_Blink();
		if(key1_pressed==1&&micro_key_work)
				{
					key1_pressed=0;
					KEY1_CNT=(KEY1_CNT+1)%3;
					switch(KEY1_CNT)
					{
					case 0: LED_Task_1bit(LED_NUM_4); 				 	break;
					case 1: LED_Task_1bit(LED_NUM_1);					break;
					case 2: LED_Task_1bit(LED_NUM_2); BUZZ_Once_ON();	break;
					default:KEY1_CNT=0;LED_Task_1bit(LED_NUM_1);      	break;
					}
				}
		if(key2_pressed==1&&micro_key_work)
		{
			key2_pressed=0;
			KEY2_CNT=(KEY2_CNT+1)%3;
			switch(KEY2_CNT)
            {
                case 0: LED_Task_1bit(LED_NUM_2); 				 	break;
                case 1: LED_Task_1bit(LED_NUM_1);					break;
                case 2: LED_Task_1bit(LED_NUM_4); BUZZ_Once_ON();	break;
                default:KEY2_CNT=0;LED_Task_1bit(LED_NUM_1);      	break;
            }
		}
		if(key_micro_pressed==1)
		{
			key_micro_pressed=0;
			micro_key_work=!micro_key_work;
			if(micro_key_work==0)
            {
				LED_RED_OFF();
				LED_WIT_OFF();
				LED_GEN_OFF();
            }
		}
		SysTick_Delay_Nms(100);
	}
  }

